﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public static class MonAnDAO
    {
        public static QL_HuongRungEntities db = new QL_HuongRungEntities();

        public static bool themMonAn(MONAN monan)
        {
            try
            {
                db.MONAN.Add(monan);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool capNhatMonAn(MONAN monan)
        {
            try
            {
                MONAN temp = db.MONAN.Find(monan.MaMonAn);
                temp.TenMonAn = monan.TenMonAn;
                temp.DS_NVL_MONAN = monan.DS_NVL_MONAN;
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                //Exception current = ex;
                //SqlException se = null;
                //do
                //{
                //    se = current.InnerException as SqlException;
                //    current = current.InnerException;
                //}
                //while (current != null && se == null);

                return false;
            }
        }

        public static int layMaMonAnLonNhat ()
        {
            try
            {
                int maMA = db.MONAN.Max(p => p.MaMonAn);
                return maMA;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public static List<MONAN> layDSMA ()
        {
            List<MONAN> arrMA = db.MONAN.ToList();
            return arrMA;
        }

        public static MONAN layMonAnDauTien()
        {
            return db.MONAN.First();
        }

        public static MONAN layMonAn(int maMA)
        {
            return db.MONAN.Find(maMA);
        }
    }
}
